<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		 <table>
			  <tr>
				 <td><img src="" /></td>
				 <td colSpan="3">购物简易计算器</td>
			  </tr>
			  <tr>
				 <td>第一个数</td>
				 <td colSpan="3">
					 <input type="text" id="num1" />
				 </td>
			  </tr>
			  <tr>
				 <td>第二个数</td>
				 <td colSpan="3">
					 <input type="text" id="num2" />
				 </td>
			  </tr>
			  <tr>
				 <td>
					 <input type="button" value=" + " onclick="cal('+')"/>
					  <!-- <input type="button" value=" + " onclick="add()"/> -->
				 </td>
				 <td>
					 <input type="button" value=" - " onclick="cal('-')"/>
				 </td>
				 <td>
					 <input type="button" value=" * " onclick="cal('*')"/>
				 </td>
				 <td>
					 <input type="button" value=" / " onclick="cal('/')"/>
				 </td>
			  </tr>
			  <tr>
				 <td>计算结果</td>
				 <td colSpan="3">
					 <input type="text" id="result" />
				 </td>
			  </tr>
		   </table>
		   
		   <script type="text/javascript">
			   /* js中方法的参数不需要加var */
			   function cal(type) {
				   console.log(type);
				   var num1 = document.getElementById("num1").value;
				   var num2 = document.getElementById("num2").value;
				   var resultValue;
				   /* 在js的switch里面可以写字符串 */
				   switch(type) {
					   // 在js里面不区分单引号和双引号 Integer.parseInt();
					   case "+":
							resultValue = parseInt(num1) + parseInt(num2);
					   break;
					   case '-':
					   		resultValue = parseInt(num1) - parseInt(num2);
					   break;
					   case '*':
					   		resultValue = parseInt(num1) * parseInt(num2);
					   break;
					   case '/':
					   		resultValue = parseInt(num1) / parseInt(num2);
					   break;
				   }
				   
				   var resultInput = document.getElementById("result");
				   resultInput.value = resultValue;
			   }
			   
			   
				function add() {
					/* 根据id拿到input标签对象 */
					var input1 = document.getElementById("num1");
					var input2 = document.getElementById("num2");
					/* 通过input标签的属性value拿到填写在input标签里面的值，
					但是这个值是string类型的，要进行计算需要转换成数值类型*/
					var num1 = parseInt(input1.value);
					var num2 = parseInt(input2.value);
					var resultValue = num1 + num2;
					var resultInput = document.getElementById("result");
					resultInput.value = resultValue;
				}
				
				function div() {
					/* 根据id拿到input标签对象 */
					var input1 = document.getElementById("num1");
					var input2 = document.getElementById("num2");
					/* 通过input标签的属性value拿到填写在input标签里面的值，
					但是这个值是string类型的，要进行计算需要转换成数值类型*/
					var num1 = parseInt(input1.value);
					var num2 = parseInt(input2.value);
					var resultValue = num1 / num2;
					var resultInput = document.getElementById("result");
					resultInput.value = resultValue;
				}
				
				function sub() {
					var num1 = document.getElementById("num1").value;
					var num2 = document.getElementById("num2").value;
					var resultValue = parseInt(num1) - parseInt(num2);
					var resultInput = document.getElementById("result");
					resultInput.value = resultValue;
				}
				
				
		   </script>
	</body>
</html>
